package cn.itcast.usermanage.web;

import java.util.*;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import cn.itcast.usermanage.bean.EasyUIPage;
import cn.itcast.usermanage.bean.UserForm;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 通用的跳转功能
	 如果springMVC中servlet配置的是扩展名拦截,如  *.do
	 http://127.0.0.1:8080/user/page/users.do
	 此时pageName获取的是 users
	 */
	@RequestMapping(value="/page/{pageName}")
	public String toPage(@PathVariable("pageName") String pageName){
		System.out.println("pageName is "+pageName);
		return pageName;
	}

	/*@RequestMapping(value = "toList")
	public String toList() {
		return "users";
	}
	@RequestMapping(value="toAddUser")
	public String toAddUser(){
		return "user-add";
	}*/

	@RequestMapping(value = "/list")
	@ResponseBody
	public EasyUIPage queryAll(
			@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
			@RequestParam(value = "rows", defaultValue = "5") Integer pageSize) {
		EasyUIPage esayUIPage = userService.queryAllUser(pageNum,pageSize);
		return esayUIPage;
	}
	
	/**
	 * 添加用户信息
	 */
	
	@RequestMapping(value="/save")
	@ResponseBody
	public Map<String, String> save(User user){
		Map<String, String> map = new HashMap<String, String>();
//		try {
			Integer num = this.userService.addUser(user);
			if(num > 0 ){
				map.put("status", "200");
			}else{
				map.put("status", "208");
			}
//		} catch (Exception e) {
////			e.printStackTrace();
//			map.put("status", "208");
//		}
		return map;
	}
	
	@RequestMapping(value="/export/excel")
	public ModelAndView export(@RequestParam(value = "page", defaultValue = "1") Integer pageNum,
			@RequestParam(value = "rows", defaultValue = "5") Integer pageSize){
		System.out.println("UserController类中的export方法执行了,默认的file.encoding="
				+System.getProperty("file.encoding"));
		//1、查询需要导出的数据
		EasyUIPage page = userService.queryAllUser(pageNum, pageSize);
		
		ModelAndView mv = new ModelAndView();
		mv.addObject("userList", page.getRows());
		mv.setViewName("userExcel"); //定位到自定义的excel视图
		return mv;
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody//springmvc 可以自动把逗号切割编程一个数组
	public Map<String, String> deleteByIds(@RequestParam(value="ids") List<Object> ids){
		Map<String, String> map = new HashMap<String, String>();
		Integer num = userService.deleteUserByIds(ids);
		if(num > 0 ){
			map.put("status", "200");
		}else{
			map.put("status", "208");
		}
		return map;
	}


//	http://127.0.0.1:8080/user/show2?date=1999-09-09
	@RequestMapping(value="/show2")
	@ResponseStatus(value=HttpStatus.OK)
	public void test1( @DateTimeFormat(pattern="yyyy-MM-dd") @RequestParam("date") Date date){
		System.out.println(date);
	}
	
	
}
